package com.jtech.together.view.weight.dialog;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.DatePicker;

import java.util.Calendar;

/**
 * 生日选择dialog
 * Created by wuxubaiyang on 2017/5/10.
 */

public class BirthdayDialog implements DatePickerDialog.OnDateSetListener, DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
    private OnBirthdaySelectDoneListener onBirthdaySelectDoneListener;
    private OnBirthdaySelectListener onBirthdaySelectListener;
    private DatePickerDialog datePickerDialog;
    private Calendar calendar;
    private Context context;
    private long birthday = -1l;

    public BirthdayDialog(Context context) {
        this.context = context;
        calendar = Calendar.getInstance();
        //获取年月日并且实例化日期时间选择器
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        //实例化日期选择器
        datePickerDialog = new DatePickerDialog(context, this, year, month, dayOfMonth);
    }

    /**
     * 构建dialog
     *
     * @param context
     * @return
     */
    public static BirthdayDialog build(Context context) {
        return new BirthdayDialog(context);
    }

    /**
     * 设置生日选择监听
     *
     * @param onBirthdaySelectListener
     * @return
     */
    public BirthdayDialog setBirthdaySelectListener(OnBirthdaySelectListener onBirthdaySelectListener) {
        this.onBirthdaySelectListener = onBirthdaySelectListener;
        return this;
    }

    /**
     * 设置生日选择完成监听
     *
     * @param onBirthdaySelectDoneListener
     * @return
     */
    public BirthdayDialog setBirthdaySelectDoneListener(OnBirthdaySelectDoneListener onBirthdaySelectDoneListener) {
        this.onBirthdaySelectDoneListener = onBirthdaySelectDoneListener;
        return this;
    }

    /**
     * 设置默认时间
     *
     * @param timeInMillis
     * @return
     */
    public BirthdayDialog setDefaultTime(long timeInMillis) {
        if (timeInMillis == 0) {
            return this;
        }
        calendar.setTimeInMillis(timeInMillis);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        datePickerDialog = new DatePickerDialog(context, this, year, month, dayOfMonth);
        return this;
    }

    /**
     * 显示dialog
     */
    public void show() {
        //设置事件选择按键监听
        datePickerDialog.setOnCancelListener(this);
        datePickerDialog.setOnDismissListener(this);
        //显示dailog
        datePickerDialog.show();
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        //取消的话则直接清空生日
        birthday = -1l;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (birthday >= 0 && null != onBirthdaySelectDoneListener) {
            calendar.setTimeInMillis(birthday);
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            onBirthdaySelectDoneListener.onSelectDone(birthday, year, month + 1, dayOfMonth);
        }
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        calendar.set(year, month, dayOfMonth);
        birthday = calendar.getTimeInMillis();
        if (null != onBirthdaySelectListener) {
            onBirthdaySelectListener.onSelect(birthday, year, month + 1, dayOfMonth);
        }
    }

    /**
     * 生日选择监听
     */
    public interface OnBirthdaySelectListener {
        void onSelect(long birthday, int year, int month, int dayOfMonth);
    }

    /**
     * 选择完成监听
     */
    public interface OnBirthdaySelectDoneListener {
        void onSelectDone(long birthday, int year, int month, int dayOfMonth);
    }
}